package controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.http.HttpRequest;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;

@RestController
@RequestMapping("/user")
public class UserController {
    /**
     *
     * 测试转发和重定向到登录首页
     */
    @GetMapping("index.do")
    public ModelAndView diIndexUI(ModelMap map,HttpSession  session){
        String username =(String)session.getAttribute("loginer");
        String date = LocalDate.now().toString();
        map.put("date",date);
        map.put("username",username);
        return new ModelAndView("index");
    }










    //访问路径:http://localhost:8080/spring_Mvc/handleLogin.do
    @PostMapping("handleLogin.do")

    public ModelAndView handleLogin3(String username, String password, ModelMap map, HttpSession session) {

        if ("江峰".equals(username)) {
            if ("8888".equals(password)) {
              map.put("message","登陆成功");
                System.out.printf("登陆成功");
                session.setAttribute("loginer",username);
                System.out.printf("开始重定向");

              return new ModelAndView("redirect:index.do");
            }else{
              map.put("message","密码错误");
              return  new ModelAndView("message");
            }

        }
        else {
          map.put("message","用户名错误");
          return  new ModelAndView("message");


        }

    }
    /**
     * 返回登录页面
     * 访问路径：http:localhost:8080/WebApp_SpringMvc/login.do
     */






    //访问路径:http://localhost:8080/spring_Mvc/
   // @GetMapping("handleLogin.do")
    public ModelAndView handleLogin2(String username, String password) {

        if ("江峰".equals(username)) {
            if ("8888".equals(password)) {

                ModelAndView mv = new ModelAndView("message");
                mv.addObject("message","登录成功");
                return mv;


            }else{

                ModelAndView mv = new ModelAndView("message");
                mv.addObject("message","登录失败");
                return mv;
            }

            }
        else {
               ModelAndView mv = new ModelAndView("message");
               mv.addObject("message","用户名错误");
               return mv;
        }

    }


    //@GetMapping("handleLogin.do")
    public ModelAndView handleLogin(String username, String password, HttpServletRequest request) {

        if ("江峰".equals(username)) {
            if ("8888".equals(password)) {

                request.setAttribute("message", "登陆成功");
                return new ModelAndView("message");


            } else {

                request.setAttribute("message", "密码错误");
                return new ModelAndView("message");

            }


        } else {
            request.setAttribute("message", "用户名错误");
            return new ModelAndView("message");

        }

    }
    @GetMapping("login.do")
    public ModelAndView showLogin(){
        System.out.println("显示登录页面");
        return new ModelAndView("/login");
    }

}



